<template>
  <div class="app-container">
    <el-card class="operate-container" shadow="never">
      <el-form :inline="true">
        <el-form-item label="您所需要查看的门店信息！">
        </el-form-item>
      </el-form>
      <el-form :inline="true" v-show="this.flag === 'business'">
        <el-button @click="handleCreate" type="success" icon="el-icon-plus" circle></el-button>
      </el-form>
    </el-card>
    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="Loading"
      border
      fit
      highlight-current-row
    >
      <el-table-column align="center" label="序号" width="130">
        <template slot-scope="scope">
          {{ scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column label="门店名" width="300" align="center">
        <template slot-scope="scope">
          {{ scope.row.name }}
        </template>
      </el-table-column>
      <el-table-column label="门店图片" width="300" align="center">
        <template slot-scope="scope">
          <el-image
            style="width: 200px; height: 150px"
            :src="'http://localhost:8080'+scope.row.image"
            :preview-src-list="srcList"
            @click="vbs(scope.row.image)"
          >
          </el-image>
        </template>
      </el-table-column>
      <el-table-column class-name="status-col" label="门店描述" width="600" align="center">
        <template slot-scope="scope">
          {{ scope.row.description }}
        </template>
      </el-table-column>
      <el-table-column label="操作" width="auto" align="center">
        <template slot-scope="scope">
          <el-button type="primary" plain @click="route(scope.row.id)">进入店铺</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      :current-page="page"
      :total="total"
      :page-size="size"
      :page-sizes="[5, 10, 15, 20, 35, 40, 100]"
      style="padding: 30px 0; text-align: center;"
      layout="sizes, prev, pager, next, jumper, ->, total, slot"
      @current-change="fetchData"
      @size-change="changeSize"
    />
    <el-dialog :visible.sync="dialogFormVisible" title="创建新门店">
      <el-form
        :model="shop"
        label-position="left"
        label-width="90px"
        style="width: 400px; margin-left:50px;"
      >
        <el-form-item label="门店名称" prop="name">
          <el-input v-model="shop.name"></el-input>
        </el-form-item>
        <el-form-item label="门店头像" prop="image">
          <div class="hello">
            <el-upload
              class="avatar-uploader"
              action="http://localhost:8080/shop/upload"
              :show-file-list="false"
              :on-success="handleAvatarSuccess"
              :before-upload="beforeAvatarUpload"
            >
              <img v-if="imageUrl" :src="imageUrl" class="avatar" alt=""/>
              <i v-else class="el-icon-upload2 avatar-uploader-icon"></i>
            </el-upload>
          </div>
        </el-form-item>
        <el-form-item label="门店描述" prop="description">
          <el-input v-model="shop.description"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="restForm">取消</el-button>
        <el-button type="primary" @click=" createData">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getToken } from '@/utils/auth'
import { addShop, getList } from '@/api/table'

export default {
  filters: {
    statusFilter(status) {
      const statusMap = {
        published: 'success',
        draft: 'gray',
        deleted: 'danger'
      }
      return statusMap[status]
    }
  },
  data() {
    return {
      list: null,
      listLoading: true,
      shop: {},
      page: 1,
      size: 5,
      total: 0,
      srcList: [
        'https://fuss10.elemecdn.com/8/27/f01c15bb73e1ef3793e64e6b7bbccjpeg.jpeg',
        'https://fuss10.elemecdn.com/1/8e/aeffeb4de74e2fde4bd74fc7b4486jpeg.jpeg'
      ],
      flag: '',
      dialogFormVisible: false,
      imageUrl: ''
    }
  },
  created() {
    this.fetchData()
    this.viewByTokenFlag()
  },
  methods: {
    // 当页码发生改变的时候
    changeSize(size) {
      this.size = size
      this.fetchData(1)
    },
    // 查询门店
    fetchData(page = 1) {
      this.page = page
      this.listLoading = true
      let token = getToken()
      let userId = JSON.parse(token).id
      getList(this.page, this.size, userId).then(response => {
        this.list = response.data.records
        this.total = response.data.total
        this.listLoading = false
      })
    },
    // 根据角色展示不同视图
    viewByTokenFlag() {
      let token = getToken()
      let parseToken = JSON.parse(token)
      this.flag = parseToken.flag
    },
    // 点击预览大图
    vbs(val) {
      this.srcList = []
      val = 'http://localhost:8080' + val
      this.srcList.push(val)
    },
    // 创建弹出
    handleCreate() {
      this.shop = {}
      this.dialogFormVisible = true
    },
    // 重置添加表单
    restForm() {
      this.dialogFormVisible = false
      this.imageUrl = ''
    },
    // 添加门店
    createData() {
      let token = getToken()
      this.shop.userId = JSON.parse(token).id
      addShop(this.shop).then(response => {
        this.dialogFormVisible = false
        this.$message({
          message: '添加成功',
          type: 'success'
        })
        this.fetchData()
      }).catch(() => {
        this.$message.error('出现故障，添加失败！')
        this.dialogFormVisible = false
      })
    },
    // 上传
    handleAvatarSuccess(res, file) {
      this.item.image = res.toString().split('resources')[1]
      this.imageUrl = URL.createObjectURL(file.raw)
    },
    // 校验
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg'
      const isLt2M = file.size / 1024 / 1024 < 2

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!')
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!')
      }
      return isJPG && isLt2M
    },
    // 路由跳转传参
    route(shopId) {
      this.$router.push({ path: 'tree', query: { id: shopId } })
    }
  }
}
</script>
<style>
.time {
  font-size: 13px;
  color: #999;
}

.bottom {
  margin-top: 13px;
  line-height: 12px;
}

.button {
  padding: 0;
  float: right;
}

.image {
  width: 100%;
  display: block;
}

.clearfix:before,
.clearfix:after {
  display: table;
  content: "";
}

.clearfix:after {
  clear: both
}
</style>
